package com.hundsun.fcp.base;
/******************************************************************
系统名称:  
类  名  称: BaseProtectActivity.java
软件版权: 恒生电子股份有限公司
系统版本: 1.0
开发人员: maqf
开发时间: 2015-05-04
功能说明: 最上层父类
审核人员: 
相关文档: 
修改记录: 需求编号         修改日期         修改人员                     修改说明
 
 ******************************************************************/
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;

import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.logger.Logger;
import com.hundsun.fcp.manager.ActivityManager;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.manager.SettingManager.FcpUserSetting;
import com.hundsun.fcp.widget.lockpattern.LockPatternUtils;
import com.umeng.analytics.MobclickAgent;

public class BaseProtectActivity extends ActionBarActivity{
	private static Logger logger = Logger.getLogger();
	private LockPatternUtils utils;
	private static long LOCK_TIME_OUT = 90*1000L;
	private static long lockTime;
	private	static boolean needLock = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().pushActivity(this) ;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this) ;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this) ;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityManager.getInstance().popActivity(this) ;
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		logger.error(  "onWindowFocusChanged ="+hasFocus+" needLock="+needLock);
		if(hasFocus){
			checkLockPattern();
		}
	}
	
	private void checkLockPattern() {
		if(isNeedLock()){
			if(!FcpUserManager.getInstance().hasLoginUser()){
				return ;
			}
				
			if (this.utils == null) {
				this.utils = new LockPatternUtils(this);
			}
			
			if (FcpUserSetting.hasLockPattern()&&utils.savedPatternExists()
					&& (System.currentTimeMillis() - lockTime) > LOCK_TIME_OUT) {
				 Redirect.startUnLockActivity(this) ;
			}
		}
	}
	
	 public static void lockPattern()
	  {
	    lockTime = 0L;
	  }
	  
	  public static void unlockPattern()
	  {
		FcpUserSetting.setLockErrorCount(0) ;
		needLock = false ;
	    lockTime = System.currentTimeMillis();
	  }
	  
	  
	  public static void quitApplication() {
		  lockPattern() ;
		  ActivityManager.getInstance().popAllActivity() ;
	  }
	  
	  public boolean isNeedLock() {
			return needLock;
		}
		public static void setNeedLock(boolean pNeedLock) {
			needLock = pNeedLock;
		}
		
		public static void setLockTime(){
			lockTime = System.currentTimeMillis();
		}
		
		/**
		 * 禁止点击菜单键
		 */
		@Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			if (keyCode == KeyEvent.KEYCODE_MENU) {
				// do nothing
				return true;
			}
			return super.onKeyDown(keyCode, event);
		}
}
